-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir] Migrate away from ArrayRef(std::nullopt) (NFC) #145140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir] Migrate away from ArrayRef(std::nullopt) (NFC) #145140
Conversation
ArrayRef has a constructor that accepts std::nullopt. This constructor dates back to the days when we still had llvm::Optional. Since the use of std::nullopt outside the context of std::optional is kind of abuse and not intuitive to new comers, I would like to move away from the constructor and eventually remove it. This patch takes care of the mlir side of the migration, starting with straightforward places like "return std::nullopt;" and ternally expressions involving std::nullopt.
|
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir-ods Author: Kazu Hirata (kazutakahirata) ChangesArrayRef has a constructor that accepts std::nullopt. This Since the use of std::nullopt outside the context of std::optional is This patch takes care of the mlir side of the migration, starting with Full diff: https://github.com/llvm/llvm-project/pull/145140.diff 8 Files Affected:
diff --git a/mlir/include/mlir/CAPI/Wrap.h b/mlir/include/mlir/CAPI/Wrap.h
index 5b68f417a3df4..fd5b6e18d4952 100644
--- a/mlir/include/mlir/CAPI/Wrap.h
+++ b/mlir/include/mlir/CAPI/Wrap.h
@@ -44,7 +44,7 @@ static llvm::ArrayRef<CppTy> unwrapList(size_t size, CTy *first,
"incompatible C and C++ types");
if (size == 0)
- return std::nullopt;
+ return {};
assert(storage.empty() && "expected to populate storage");
storage.reserve(size);
diff --git a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
index 1e108c3d8ac77..6ee638c19d1ad 100644
--- a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
+++ b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
@@ -360,12 +360,13 @@ def PDL_OperationOp : PDL_Op<"operation", [AttrSizedOperandSegments]> {
(`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict
}];
- let builders = [
- OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
- CArg<"ValueRange", "std::nullopt">:$operandValues,
- CArg<"ArrayRef<StringRef>", "std::nullopt">:$attrNames,
- CArg<"ValueRange", "std::nullopt">:$attrValues,
- CArg<"ValueRange", "std::nullopt">:$resultTypes), [{
+ let builders =
+ [OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
+ CArg<"ValueRange", "{}">:$operandValues,
+ CArg<"ArrayRef<StringRef>", "{}">:$attrNames,
+ CArg<"ValueRange", "{}">:$attrValues,
+ CArg<"ValueRange", "{}">:$resultTypes),
+ [{
auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr();
build($_builder, $_state, $_builder.getType<OperationType>(), nameAttr,
operandValues, attrValues, $_builder.getStrArrayAttr(attrNames),
diff --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td
index 8855908276500..b67b8f939b9ee 100644
--- a/mlir/include/mlir/IR/BuiltinAttributes.td
+++ b/mlir/include/mlir/IR/BuiltinAttributes.td
@@ -535,9 +535,8 @@ def Builtin_DictionaryAttr : Builtin_Attr<"Dictionary", "dictionary"> {
```
}];
let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value);
- let builders = [
- AttrBuilder<(ins CArg<"ArrayRef<NamedAttribute>", "std::nullopt">:$value)>
- ];
+ let builders = [AttrBuilder<(
+ ins CArg<"ArrayRef<NamedAttribute>", "{}">:$value)>];
let extraClassDeclaration = [{
using ValueType = ArrayRef<NamedAttribute>;
diff --git a/mlir/include/mlir/Support/StorageUniquer.h b/mlir/include/mlir/Support/StorageUniquer.h
index 6756c4390276f..b5f4df680ac69 100644
--- a/mlir/include/mlir/Support/StorageUniquer.h
+++ b/mlir/include/mlir/Support/StorageUniquer.h
@@ -97,7 +97,7 @@ class StorageUniquer {
template <typename T>
ArrayRef<T> copyInto(ArrayRef<T> elements) {
if (elements.empty())
- return std::nullopt;
+ return {};
auto result = allocator.Allocate<T>(elements.size());
llvm::uninitialized_copy(elements, result);
return ArrayRef<T>(result, elements.size());
diff --git a/mlir/lib/Interfaces/FunctionInterfaces.cpp b/mlir/lib/Interfaces/FunctionInterfaces.cpp
index 57a8668117c68..e0f1135e992ac 100644
--- a/mlir/lib/Interfaces/FunctionInterfaces.cpp
+++ b/mlir/lib/Interfaces/FunctionInterfaces.cpp
@@ -44,14 +44,14 @@ function_interface_impl::getResultAttrDict(FunctionOpInterface op,
ArrayRef<NamedAttribute>
function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) {
auto argDict = getArgAttrDict(op, index);
- return argDict ? argDict.getValue() : std::nullopt;
+ return argDict ? argDict.getValue() : ArrayRef<NamedAttribute>();
}
ArrayRef<NamedAttribute>
function_interface_impl::getResultAttrs(FunctionOpInterface op,
unsigned index) {
auto resultDict = getResultAttrDict(op, index);
- return resultDict ? resultDict.getValue() : std::nullopt;
+ return resultDict ? resultDict.getValue() : ArrayRef<NamedAttribute>();
}
/// Get either the argument or result attributes array.
diff --git a/mlir/lib/Tools/PDLL/Parser/Parser.cpp b/mlir/lib/Tools/PDLL/Parser/Parser.cpp
index c0e2252bdebc5..ad7d71b0bdd2f 100644
--- a/mlir/lib/Tools/PDLL/Parser/Parser.cpp
+++ b/mlir/lib/Tools/PDLL/Parser/Parser.cpp
@@ -2883,8 +2883,9 @@ Parser::validateOperationOperands(SMRange loc, std::optional<StringRef> name,
SmallVectorImpl<ast::Expr *> &operands) {
return validateOperationOperandsOrResults(
"operand", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
- operands, odsOp ? odsOp->getOperands() : std::nullopt, valueTy,
- valueRangeTy);
+ operands,
+ odsOp ? odsOp->getOperands() : ArrayRef<pdll::ods::OperandOrResult>(),
+ valueTy, valueRangeTy);
}
LogicalResult
@@ -2893,7 +2894,9 @@ Parser::validateOperationResults(SMRange loc, std::optional<StringRef> name,
SmallVectorImpl<ast::Expr *> &results) {
return validateOperationOperandsOrResults(
"result", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
- results, odsOp ? odsOp->getResults() : std::nullopt, typeTy, typeRangeTy);
+ results,
+ odsOp ? odsOp->getResults() : ArrayRef<pdll::ods::OperandOrResult>(),
+ typeTy, typeRangeTy);
}
void Parser::checkOperationResultTypeInferrence(SMRange loc, StringRef opName,
diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
index c39540da396b6..84f529ae16401 100644
--- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
+++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
@@ -1044,7 +1044,8 @@ class LSPSignatureHelpContext : public CodeCompleteContext {
const ods::Operation *odsOp =
opName ? odsContext.lookupOperation(*opName) : nullptr;
codeCompleteOperationOperandOrResultSignature(
- opName, odsOp, odsOp ? odsOp->getOperands() : std::nullopt,
+ opName, odsOp,
+ odsOp ? odsOp->getOperands() : ArrayRef<ods::OperandOrResult>(),
currentNumOperands, "operand", "Value");
}
@@ -1053,7 +1054,8 @@ class LSPSignatureHelpContext : public CodeCompleteContext {
const ods::Operation *odsOp =
opName ? odsContext.lookupOperation(*opName) : nullptr;
codeCompleteOperationOperandOrResultSignature(
- opName, odsOp, odsOp ? odsOp->getResults() : std::nullopt,
+ opName, odsOp,
+ odsOp ? odsOp->getResults() : ArrayRef<ods::OperandOrResult>(),
currentNumResults, "result", "Type");
}
diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index 80661e68754ce..4f6655d0b2978 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -194,7 +194,7 @@ void TestSubElementsAccessAttr::print(::mlir::AsmPrinter &printer) const {
ArrayRef<uint64_t> TestExtern1DI64ElementsAttr::getElements() const {
if (auto *blob = getHandle().getBlob())
return blob->getDataAs<uint64_t>();
- return std::nullopt;
+ return {};
}
//===----------------------------------------------------------------------===//
|
|
@llvm/pr-subscribers-mlir-pdl Author: Kazu Hirata (kazutakahirata) ChangesArrayRef has a constructor that accepts std::nullopt. This Since the use of std::nullopt outside the context of std::optional is This patch takes care of the mlir side of the migration, starting with Full diff: https://github.com/llvm/llvm-project/pull/145140.diff 8 Files Affected:
diff --git a/mlir/include/mlir/CAPI/Wrap.h b/mlir/include/mlir/CAPI/Wrap.h
index 5b68f417a3df4..fd5b6e18d4952 100644
--- a/mlir/include/mlir/CAPI/Wrap.h
+++ b/mlir/include/mlir/CAPI/Wrap.h
@@ -44,7 +44,7 @@ static llvm::ArrayRef<CppTy> unwrapList(size_t size, CTy *first,
"incompatible C and C++ types");
if (size == 0)
- return std::nullopt;
+ return {};
assert(storage.empty() && "expected to populate storage");
storage.reserve(size);
diff --git a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
index 1e108c3d8ac77..6ee638c19d1ad 100644
--- a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
+++ b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
@@ -360,12 +360,13 @@ def PDL_OperationOp : PDL_Op<"operation", [AttrSizedOperandSegments]> {
(`->` `(` $typeValues^ `:` type($typeValues) `)`)? attr-dict
}];
- let builders = [
- OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
- CArg<"ValueRange", "std::nullopt">:$operandValues,
- CArg<"ArrayRef<StringRef>", "std::nullopt">:$attrNames,
- CArg<"ValueRange", "std::nullopt">:$attrValues,
- CArg<"ValueRange", "std::nullopt">:$resultTypes), [{
+ let builders =
+ [OpBuilder<(ins CArg<"std::optional<StringRef>", "std::nullopt">:$name,
+ CArg<"ValueRange", "{}">:$operandValues,
+ CArg<"ArrayRef<StringRef>", "{}">:$attrNames,
+ CArg<"ValueRange", "{}">:$attrValues,
+ CArg<"ValueRange", "{}">:$resultTypes),
+ [{
auto nameAttr = name ? $_builder.getStringAttr(*name) : StringAttr();
build($_builder, $_state, $_builder.getType<OperationType>(), nameAttr,
operandValues, attrValues, $_builder.getStrArrayAttr(attrNames),
diff --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td
index 8855908276500..b67b8f939b9ee 100644
--- a/mlir/include/mlir/IR/BuiltinAttributes.td
+++ b/mlir/include/mlir/IR/BuiltinAttributes.td
@@ -535,9 +535,8 @@ def Builtin_DictionaryAttr : Builtin_Attr<"Dictionary", "dictionary"> {
```
}];
let parameters = (ins ArrayRefParameter<"NamedAttribute", "">:$value);
- let builders = [
- AttrBuilder<(ins CArg<"ArrayRef<NamedAttribute>", "std::nullopt">:$value)>
- ];
+ let builders = [AttrBuilder<(
+ ins CArg<"ArrayRef<NamedAttribute>", "{}">:$value)>];
let extraClassDeclaration = [{
using ValueType = ArrayRef<NamedAttribute>;
diff --git a/mlir/include/mlir/Support/StorageUniquer.h b/mlir/include/mlir/Support/StorageUniquer.h
index 6756c4390276f..b5f4df680ac69 100644
--- a/mlir/include/mlir/Support/StorageUniquer.h
+++ b/mlir/include/mlir/Support/StorageUniquer.h
@@ -97,7 +97,7 @@ class StorageUniquer {
template <typename T>
ArrayRef<T> copyInto(ArrayRef<T> elements) {
if (elements.empty())
- return std::nullopt;
+ return {};
auto result = allocator.Allocate<T>(elements.size());
llvm::uninitialized_copy(elements, result);
return ArrayRef<T>(result, elements.size());
diff --git a/mlir/lib/Interfaces/FunctionInterfaces.cpp b/mlir/lib/Interfaces/FunctionInterfaces.cpp
index 57a8668117c68..e0f1135e992ac 100644
--- a/mlir/lib/Interfaces/FunctionInterfaces.cpp
+++ b/mlir/lib/Interfaces/FunctionInterfaces.cpp
@@ -44,14 +44,14 @@ function_interface_impl::getResultAttrDict(FunctionOpInterface op,
ArrayRef<NamedAttribute>
function_interface_impl::getArgAttrs(FunctionOpInterface op, unsigned index) {
auto argDict = getArgAttrDict(op, index);
- return argDict ? argDict.getValue() : std::nullopt;
+ return argDict ? argDict.getValue() : ArrayRef<NamedAttribute>();
}
ArrayRef<NamedAttribute>
function_interface_impl::getResultAttrs(FunctionOpInterface op,
unsigned index) {
auto resultDict = getResultAttrDict(op, index);
- return resultDict ? resultDict.getValue() : std::nullopt;
+ return resultDict ? resultDict.getValue() : ArrayRef<NamedAttribute>();
}
/// Get either the argument or result attributes array.
diff --git a/mlir/lib/Tools/PDLL/Parser/Parser.cpp b/mlir/lib/Tools/PDLL/Parser/Parser.cpp
index c0e2252bdebc5..ad7d71b0bdd2f 100644
--- a/mlir/lib/Tools/PDLL/Parser/Parser.cpp
+++ b/mlir/lib/Tools/PDLL/Parser/Parser.cpp
@@ -2883,8 +2883,9 @@ Parser::validateOperationOperands(SMRange loc, std::optional<StringRef> name,
SmallVectorImpl<ast::Expr *> &operands) {
return validateOperationOperandsOrResults(
"operand", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
- operands, odsOp ? odsOp->getOperands() : std::nullopt, valueTy,
- valueRangeTy);
+ operands,
+ odsOp ? odsOp->getOperands() : ArrayRef<pdll::ods::OperandOrResult>(),
+ valueTy, valueRangeTy);
}
LogicalResult
@@ -2893,7 +2894,9 @@ Parser::validateOperationResults(SMRange loc, std::optional<StringRef> name,
SmallVectorImpl<ast::Expr *> &results) {
return validateOperationOperandsOrResults(
"result", loc, odsOp ? odsOp->getLoc() : std::optional<SMRange>(), name,
- results, odsOp ? odsOp->getResults() : std::nullopt, typeTy, typeRangeTy);
+ results,
+ odsOp ? odsOp->getResults() : ArrayRef<pdll::ods::OperandOrResult>(),
+ typeTy, typeRangeTy);
}
void Parser::checkOperationResultTypeInferrence(SMRange loc, StringRef opName,
diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
index c39540da396b6..84f529ae16401 100644
--- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
+++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
@@ -1044,7 +1044,8 @@ class LSPSignatureHelpContext : public CodeCompleteContext {
const ods::Operation *odsOp =
opName ? odsContext.lookupOperation(*opName) : nullptr;
codeCompleteOperationOperandOrResultSignature(
- opName, odsOp, odsOp ? odsOp->getOperands() : std::nullopt,
+ opName, odsOp,
+ odsOp ? odsOp->getOperands() : ArrayRef<ods::OperandOrResult>(),
currentNumOperands, "operand", "Value");
}
@@ -1053,7 +1054,8 @@ class LSPSignatureHelpContext : public CodeCompleteContext {
const ods::Operation *odsOp =
opName ? odsContext.lookupOperation(*opName) : nullptr;
codeCompleteOperationOperandOrResultSignature(
- opName, odsOp, odsOp ? odsOp->getResults() : std::nullopt,
+ opName, odsOp,
+ odsOp ? odsOp->getResults() : ArrayRef<ods::OperandOrResult>(),
currentNumResults, "result", "Type");
}
diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index 80661e68754ce..4f6655d0b2978 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -194,7 +194,7 @@ void TestSubElementsAccessAttr::print(::mlir::AsmPrinter &printer) const {
ArrayRef<uint64_t> TestExtern1DI64ElementsAttr::getElements() const {
if (auto *blob = getHandle().getBlob())
return blob->getDataAs<uint64_t>();
- return std::nullopt;
+ return {};
}
//===----------------------------------------------------------------------===//
|
ArrayRef has a constructor that accepts std::nullopt. This
constructor dates back to the days when we still had llvm::Optional.
Since the use of std::nullopt outside the context of std::optional is
kind of abuse and not intuitive to new comers, I would like to move
away from the constructor and eventually remove it.
One of the common uses of std::nullopt is in one of the constructors
for ValueRange. This patch takes care of the migration where we need
ValueRange() to facilitate perfect forwarding. Note that {} would be
ambiguous for perfecting forwarding to work.
ArrayRef has a constructor that accepts std::nullopt. This
constructor dates back to the days when we still had llvm::Optional.
Since the use of std::nullopt outside the context of std::optional is
kind of abuse and not intuitive to new comers, I would like to move
away from the constructor and eventually remove it.
This patch takes care of the mlir side of the migration, starting with
straightforward places like "return std::nullopt;" and ternally
expressions involving std::nullopt.